package com.treehouse.northlight.system;

import com.artemis.Aspect;
import com.artemis.ComponentMapper;
import com.artemis.Entity;
import com.artemis.systems.EntityProcessingSystem;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.treehouse.northlight.component.Playable;
import com.treehouse.northlight.component.SelectionBox;
import com.treehouse.northlight.component.TransformWrapper;

/**
 * System Class.
 * 
 * Manages the user input.
 * 
 * @author Julian Biermann
 * @version 1.0 04.03.2013 08:57:40
 * 
 */
public class PlayerInputSystem extends EntityProcessingSystem {

	private ComponentMapper<SelectionBox> selectionMapper;
	private ComponentMapper<TransformWrapper> transformWrapper;

	private boolean isTouched;
	private int left = Keys.A;
	private int top = Keys.W;
	private int right = Keys.D;
	private int down = Keys.S;
	private boolean isLeft;
	private boolean isRight;
	private boolean isUp;
	private boolean isDown;
	private float x;
	private float y;

	/**
	 * Constructor
	 */
	@SuppressWarnings("unchecked")
	public PlayerInputSystem() {
		super(Aspect.getAspectForAll(Playable.class, SelectionBox.class));
		this.isTouched = false;
		this.isLeft = false;
		this.isRight = false;
		this.isUp = false;
		this.isDown = false;
		this.x = 0;
		this.y = 0;
	}

	@Override
	protected void initialize() {
		this.selectionMapper = world.getMapper(SelectionBox.class);
		this.transformWrapper = world.getMapper(TransformWrapper.class);
	}

	/**
	 * @see com.artemis.EntitySystem#begin()
	 */
	@Override
	protected void begin() {
		this.isTouched = Gdx.input.isTouched();
		this.isLeft = Gdx.input.isKeyPressed(this.left);
		this.isUp = Gdx.input.isKeyPressed(this.top);
		this.isRight = Gdx.input.isKeyPressed(this.right);
		this.isDown = Gdx.input.isKeyPressed(this.down);
		this.x = Gdx.input.getX();
		this.y = Gdx.input.getY();
	}

	@Override
	protected void process(Entity e) {
		SelectionBox box = this.selectionMapper.get(e);
		TransformWrapper trs = this.transformWrapper.get(e);

		if (this.isTouched) {
			if (box.getRectangle().contains(x, y)) {
				System.out.println(e.getId() + " selected!");
			}
		}

		if (this.isUp) {
			trs.getTransform().setPosition(
					trs.getTransform().getPosition().add(0f, -0.5f));
			box.setX(trs.getTransform().getPosition().x);
			box.setY(trs.getTransform().getPosition().y);
		}

		if (this.isDown) {
			trs.getTransform().setPosition(
					trs.getTransform().getPosition().add(0f, 0.5f));
			box.setX(trs.getTransform().getPosition().x);
			box.setY(trs.getTransform().getPosition().y);
		}

		if (this.isLeft) {
			trs.getTransform().setPosition(
					trs.getTransform().getPosition().add(-0.5f, 0f));
			box.setX(trs.getTransform().getPosition().x);
			box.setY(trs.getTransform().getPosition().y);
		}

		if (this.isRight) {
			trs.getTransform().setPosition(
					trs.getTransform().getPosition().add(0.5f, 0f));
			box.setX(trs.getTransform().getPosition().x);
			box.setY(trs.getTransform().getPosition().y);
		}
	}

	@Override
	protected boolean checkProcessing() {
		return true;
	}
}
